﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
using System.Data.SqlClient;
using System.Data;

namespace Cashier
{
    /// <summary>
    /// OderNotPage.xaml 的交互逻辑
    /// </summary>
    public partial class OderNotPage : Page
    {
        public OderNotPage()
        {
            InitializeComponent();
            ShowOders();

        }
        private DataTable data;

        private void ShowOders()
        {
            if (data != null) {
                 data.Clear();
            }
            listView.Items.Clear();
            try
            {
                //获取表格
                data = new DataTable("oder");
                data.Columns.Add(new DataColumn("oder_id", typeof(string)));
                data.Columns.Add(new DataColumn("sum", typeof(string)));
                data.Columns.Add(new DataColumn("table_id", typeof(string)));
                using (SQLiteConnection conn = new SQLiteConnection(CommonValue.sqliteConnectString))
                {
                    conn.Open();
                    string cmd = "select * from oder where complete=0";
                    using (SQLiteCommand myCmd = new SQLiteCommand(cmd, conn))
                    {
                        //SQLiteDataAdapter mda = new SQLiteDataAdapter(cmd, conn);
                        using (SQLiteDataReader reader = myCmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {

                                string id = reader["oder_id"].ToString();
                                string name = reader["sum"].ToString();
                                string table_id = reader["table_id"].ToString();
                                UnPayOrder unPayOrder = new UnPayOrder(id, name, table_id);
                                data.Rows.Add(id, name, table_id);
                                listView.Items.Add(unPayOrder);
                            }
                            //listView.DataContext = data.DefaultView;
                            conn.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("查询订单失败:" + e.Message);
            }

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            CommonValue.FOOD_PAY_ID = int.Parse(idPayBox.Text.ToString());
            NavigationWindow window = new NavigationWindow();

            window.Source = new Uri("OderDetailPage.xaml", UriKind.Relative);
            window.Closed += Window_Closed;
            window.ShowDialog();
            
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            ShowOders();
        }

        private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = listView.SelectedIndex;
            if (listView.SelectedIndex < 0 || listView.SelectedIndex >= listView.Items.Count) return;
            if (index >= 0 && index < data.Rows.Count) {
                idPayBox.Text = data.Rows[listView.SelectedIndex].ItemArray[0].ToString();
            }
            
        }
    }
}
